package com.gxa.meiye.admin.platform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxa.meiye.admin.platform.pojo.domain.User;
import com.gxa.meiye.admin.platform.pojo.vo.AddWebUserVo;
import com.gxa.meiye.admin.platform.pojo.vo.WebUserVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface WebUserMapper extends BaseMapper<WebUserVo> {
    @Select("SELECT u.id,u.account,u.name,r.name AS role_name,u.phone,u.status\n" +
            "        FROM t_web_user u,t_web_role r,t_web_user_role ur\n" +
            "        WHERE u.id=ur.user_id AND r.id = ur.role_id")
    List<Map<String,Object>> webUserList();


    @Select("SELECT name FROM t_web_role")
    List<String> roleList();

    @Insert("INSERT INTO t_web_user(`account`, `pwd`, `phone`, `status`, `name`) VALUES (#{account}, #{pwd}, #{phone}, #{status}, #{name});")
    @SelectKey(statement="select LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Integer.class)
    Integer add(User user);

    @Select("SELECT id FROM t_web_role WHERE name=#{name}")
    Integer selectRoleId(String name);

    @Insert("INSERT INTO `meiye`.`t_web_user_role`(`user_id`, `role_id`) VALUES (#{user_id}, #{role_id});")
    Integer addUserRole(Integer user_id, Integer role_id);

    @Select("SELECT u.id,u.account,u.name,u.phone,u.status\n" +
            "        FROM t_web_user u\n" +
            "        WHERE  u.name LIKE #{select} OR u.account LIKE #{select}")
    List<User> queryList(String select);

    @Select("SELECT name FROM t_web_role r,t_web_user_role ur " +
            "WHERE r.id=ur.role_id AND ur.user_id=#{id}")
    List<String> selectNameByUserId(Integer user_id);

    @Delete("DELETE FROM t_web_user_role WHERE user_id = #{id}")
    void delUserRole(Integer id);

    @Update("UPDATE `t_web_user` SET `account` = #{account}, `pwd` = #{pwd}, `phone` = #{phone}, `status` = #{status}, `name` = #{name} WHERE `id` = #{id}")
    Integer update(AddWebUserVo webUserVo);

    @Delete("DELETE FROM t_web_user_role WHERE user_id = #{user_id}")
    Integer delUserRoleId(Integer user_id, String name);
}
